﻿Imports System.IO
Imports System.Net
Imports System.Text

Public Class HttpHelper

    Public Function HttpGet(Url As String, postDataStr As String) As String
        Dim request As HttpWebRequest = WebRequest.Create(Url + postDataStr)
        request.Method = "GET"
        request.ContentType = "text/html;charset=UTF-8"

        Dim response As HttpWebResponse = request.GetResponse()
        Dim myResponseStream As Stream = response.GetResponseStream()
        Dim myStreamReader As StreamReader = New StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"))
        Dim retString As String = myStreamReader.ReadToEnd()
        myStreamReader.Close()
        myResponseStream.Close()

        Return retString
    End Function

    Public Function HttpPostWebService(method As String, param As String) As String
        Dim mes_switch As String = String.Empty
        Dim mes_website As String = String.Empty
        Dim objClsSysParam As New clsSysParam
        Dim dtsw As DataTable = objClsSysParam.selectMesSwitch()
        If dtsw.Rows.Count = 1 Then
            mes_switch = dtsw.Rows(0)("param_value").ToString()
        End If
        Dim dt As DataTable = objClsSysParam.selectMesWebSite()
        If dt.Rows.Count = 1 Then
            mes_website = dt.Rows(0)("param_value").ToString()
        End If
        If String.IsNullOrEmpty(mes_switch) = False Then
            If mes_switch.Equals("1") Then
                If String.IsNullOrEmpty(mes_website) = False Then
                    Dim url As String = mes_website & "newmes/services/WSAccessoriesWebService"
                    Dim params As String = "<soap:Envelope xmlns:soap=""http://www.w3.org/2003/05/soap-envelope"" xmlns:web=""http://webservice.leanway.com"">
           <soap:Header/>
           <soap:Body>
              <web:{0}>
                 <web:param>{1}</web:param>
              </web:{2}>
           </soap:Body>
        </soap:Envelope>"
                    Dim postDataStr As String = String.Format(params, method, param, method)
                    Dim rtn As String = HttpPost(url, postDataStr)
                    Dim rpl1 As String = "<?xml version='1.0' encoding='UTF-8'?><soapenv:Envelope xmlns:soapenv=""http://www.w3.org/2003/05/soap-envelope""><soapenv:Body><ns:" & method & "Response xmlns:ns=""http://webservice.leanway.com""><ns:return>"
                    Dim rpl2 As String = "</ns:return></ns:" & method & "Response></soapenv:Body></soapenv:Envelope>"
                    Dim rtns As String = rtn.Replace(rpl1, "").Replace(rpl2, "")
                    Return rtns
                Else
                    Return "请联系工作人员，配置mes网址"
                End If
            Else
                Return "mes接口开关当前为关闭状态，如需使用，请联系工作人员，将mes接口开关打开。"
            End If
        Else
            Return "请联系工作人员，配置mes接口开关"
        End If
    End Function


    Public Function HttpPost(Url As String, postDataStr As String) As String
        Dim request As HttpWebRequest = WebRequest.Create(Url)
        request.Method = "POST"
        request.ContentType = "application/soap+xml"
        Dim postBytes As Byte() = Encoding.UTF8.GetBytes(postDataStr)
        request.ContentLength = postBytes.Length
        Dim myRequestStream As Stream = request.GetRequestStream()
        myRequestStream.Write(postBytes, 0, postBytes.Length)
        myRequestStream.Close()

        Dim response As HttpWebResponse = request.GetResponse()

        Dim myResponseStream As Stream = response.GetResponseStream()
        Dim myStreamReader As StreamReader = New StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"))
        Dim retString As String = myStreamReader.ReadToEnd()
        myStreamReader.Close()
        myResponseStream.Close()

        Return retString
    End Function

End Class
